package chances.epg.ui.taglib.html;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.exception.VelocityException;

import com.google.common.base.Strings;

import chances.epg.commons.utils.DateUtils;
import chances.epg.commons.utils.JSONUtils;
import chances.epg.commons.utils.PathUtils;
import chances.epg.ui.taglib.model.ContentModel;
import chances.epg.ui.taglib.model.EffectModel;
import chances.epg.ui.taglib.template.TemplateFactory;

@SuppressWarnings("serial")
public abstract class EpgBodyTagSupport extends EpgTagSupport {
	public static String DEFALUT_THEME = "default";
	private EffectModel effect = new EffectModel();
	private ContentModel content = new ContentModel();

	private String effectOpts;
	private String contentOpts;

	private String style;
	private String onfocus;
	private String onblur;
	private String onkeypress;
	private String theme;
	protected String vm;
	private boolean inner = false;
	private boolean display = true;

	public void doAfterStart() throws JspException {
		if (Strings.isNullOrEmpty(this.getVm())) {
			this.vm = this.getClass().getSimpleName().replaceAll("Tag", "").toLowerCase();
		}
	};

	@Override
	public int doStartTag() throws JspException {
		if (!this.display) {

			return SKIP_BODY;
		}

		doAfterStart();

		if (StringUtils.isEmpty(this.id)) {
			Tag aLinkTag = this.getParent();
			if (aLinkTag != null && BodyTagSupport.class.isInstance(aLinkTag)) {
				this.id = ((BodyTagSupport) aLinkTag).getId() + "_" + this.vm + "_" + epgPageContext.nextId();

			} else {
				this.id = "epg_" + this.vm + "_" + epgPageContext.nextId();
			}

		}

		return super.doStartTag();
	}


	@Override
	public int doAfterBody() throws JspException {

		return super.doAfterBody();
	}

	@Override
	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	@Override
	public int doEndTag() throws JspException {
		if (this.display) {
			this.applyTemplate();
		}
		this.release();
		return EVAL_PAGE;
	}

	public LinkTag findLinkTag() {
		return (LinkTag) findAncestorWithClass(this, LinkTag.class);
	}

	@Override
	public void release() {
		effect = new EffectModel();
		content = new ContentModel();
		effectOpts = null;
		contentOpts = null;
		style = null;
		this.theme =null;
		this.vm = null;
		this.id = null;
		inner = false;
		this.display = true;
	}

	/**
	 * 
	 * @param templateName
	 * @throws IOException
	 * @throws VelocityException
	 */
	protected void applyTemplate() throws JspException {
		if (this.inner) {
			return;
		}
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			addParameter(params);
			params.put("tag", this);
			if (this.bodyContent != null) {
				params.put("content", this.bodyContent.getString());
			}
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			params.put("_contextPath", request.getContextPath());
			params.put("_pagePath", request.getServletPath());
			params.put("epgPageContext", epgPageContext);
			params.put("JSONUtils", JSONUtils.class);
			params.put("PathUtils", PathUtils.class);
			params.put("DateUtils", DateUtils.class);
			params.putAll(params);
			TemplateFactory.getInstance(pageContext.getServletContext()).merge(params, this);

		} catch (java.lang.Throwable e) {
			throw new JspException(e);
		}
	}

	public void addParameter(Map<String, Object> params) {
		// nothing
	}

	public String getTheme() {
		if(theme==null){
			theme = epgPageContext.getSiteCode(); 
			
		}
		if(theme==null){
			theme =DEFALUT_THEME;
		}
		return theme;
	}

	public String getVm() {
		return vm;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public void setVm(String vm) {
		this.vm = vm;
	}

	public String getFocusClass() {
		return effect.getFocusClass();
	}

	public String getCurrentClass() {
		return effect.getCurrentClass();
	}

	public String getStyleClass() {
		return effect.getStyleClass();
	}

	public String getCursor() {
		return effect.getCursor();
	}

	public int getWordSize() {
		return effect.getWordSize();
	}

	public int getScale() {
		return effect.getScale();
	}

	public int getTime() {
		return effect.getTime();
	}

	public String getEffectName() {
		return effect.getName();
	}

	public void setFocusClass(String focusClass) {
		effect.setFocusClass(focusClass);
	}

	public void setCurrentClass(String currentClass) {
		effect.setCurrentClass(currentClass);
	}

	public void setStyleClass(String styleClass) {
		effect.setStyleClass(styleClass);
	}

	public void setCursor(String cursor) {
		effect.setCursor(cursor);
	}

	public void setWordSize(int wordSize) {
		effect.setWordSize(wordSize);
	}

	public void setScale(int scale) {
		effect.setScale(scale);
	}

	public void setTime(int time) {
		effect.setTime(time);
	}

	public void setEffectName(String name) {
		effect.setName(name);
	}

	public String getTitle() {
		return content.getTitle();
	}

	public String getOnkeypress() {
		return onkeypress;
	}

	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}

	public boolean isDisplay() {
		return display;
	}

	public void setDisplay(boolean display) {
		this.display = display;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getFlagIcon() {
		return content.getFlagIcon();
	}

	public boolean isInner() {
		return inner;
	}

	public void setInner(boolean inner) {
		this.inner = inner;
	}

	public String getImage() {
		return content.getImage();
	}

	public String getSubTitle() {
		return content.getSubTitle();
	}

	public void setTitle(String title) {
		content.setTitle(title);
	}

	public void setFlagIcon(String flagIcon) {
		content.setFlagIcon(flagIcon);
	}

	public void setImage(String image) {
		content.setImage(image);
	}

	public void setSubTitle(String subTitle) {
		content.setSubTitle(subTitle);
	}

	public String getLink() {
		return content.getLink();
	}

	public void setLink(String link) {
		content.setLink(link);
	}

	public String getShortTitle() {
		return content.getShortTitle();
	}

	public void setShortTitle(String shortTitle) {
		content.setShortTitle(shortTitle);
	}

	public String getEffectOpts() {
		return effectOpts;
	}

	public String getContentOpts() {
		return contentOpts;
	}

	public void setEffectOpts(String effectOpts) {
		this.effectOpts = effectOpts;
	}

	public void setContentOpts(String contentOpts) {
		this.contentOpts = contentOpts;
	}

	public EffectModel getEffect() {
		return effect;
	}

	public ContentModel getContent() {
		return content;
	}

	public void setEffect(EffectModel effect) {
		this.effect = effect;
	}

	public void setContent(ContentModel content) {
		this.content = content;
	}

	public String getStyle() {
		return this.style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getTemplate() {
		return PathUtils.join(this.getTheme(),this.getVm());
	}

}
